/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.engine;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.SgmlPage;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.BaseFrameElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.seari.bean.IndexDoc;
import com.seari.bean.SubSite;
import com.seari.utils.HashTool;
import com.seari.utils.SpiderHelper;
import com.seari.utils.StringHelper;
import io.webfolder.cdp.Launcher;
import io.webfolder.cdp.session.Session;
import io.webfolder.cdp.session.SessionFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Rainbow
 */
public class SimpleImagePageRunner
{
    protected WebClient webClient;
    protected Session session;
    protected Logger myLogger = LoggerFactory.getLogger(SimpleImagePageRunner.class);
    protected static String workURL = "";
    protected SubSite site;
    
    private void initEngine(boolean jsEnabled,String engine)
    {
        switch(engine)
        {
            case SpiderHelper.CHROME_HEADLESS:
                List<String> args = new ArrayList<String>();
                //设置chrome代理服务器
                args.add("--proxy-server=127.0.0.1:56787");
                Launcher launcher = new Launcher();
                try 
                {
                    SessionFactory factory = launcher.launch(args);
                    session = factory.create();
                    session.navigate("https://xrba.tumblr.com/archive");
                    session.waitDocumentReady();
                    session.getOuterHtml("//body");
                    String content = (String) session.getProperty("//body", "outerText");
                    System.out.println(content);
                }catch(Exception e){
                   e.printStackTrace();
                }
                break;
            case SpiderHelper.HTMLUNIT:
                initWebclient(jsEnabled);
                break;
        }

    }
    
    private void initWebclient(boolean jsEnabled)
    {
        webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(jsEnabled);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setTimeout(60000);
        webClient.setJavaScriptTimeout(15000);
        webClient.getOptions().setProxyConfig(new ProxyConfig("127.0.0.1",56787,true));
    }
    
    private void initSubsite()
    {
        site = new SubSite();
        
    }
    
    public static void main(String[] args) throws IOException
    {
        SimpleImagePageRunner spr = new SimpleImagePageRunner();
        spr.initEngine(true, SpiderHelper.CHROME_HEADLESS);
        spr.initSubsite();
        String guid = HashTool.generateUrlGuid(workURL);
        IndexDoc doc = new IndexDoc();
        doc.url = workURL;
        doc.guid = guid;
        

    }
    
    public void getPageContentAndImageUrls(IndexDoc wa, HtmlPage sp) throws Exception
    {
    
    }
    
    public HtmlPage getArticlePage(IndexDoc at) throws Exception
    {
        HtmlPage sp = null;
        Page page = null;
        try
        {
            page = openPage(at.url);
            at.url = page.getUrl().toString();
        }
        catch (Exception e)
        {
            try
            {
                page = openPage(at.url);
            }
            catch (Exception ee)
            {
                myLogger.warn("faild to open page(url:{} - guid:{}", at.url, at.guid);
                throw ee;
            }
        }
        if (webClient.getOptions().isJavaScriptEnabled())
        {
            webClient.waitForBackgroundJavaScript(5000);
        }
        if (page instanceof HtmlPage)
        {
            sp = (HtmlPage) page;
        }
        if (sp == null)
        {
            myLogger.warn("cannot be cast html page {}", at.url);
            return null;
        }
        int httpCode = sp.getWebResponse().getStatusCode();
        if (httpCode == 404)
        {
            sp.cleanUp();
            myLogger.warn("the page return 404 code url({0}) guid({1})", at.url, at.guid);
            return null;
        }

        return sp;
    }
    
    public SgmlPage openPage(String url) throws Exception
    {
        return webClient.getPage(url);
    }
}
